package com.app.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.app.model.CustomerModel;
import com.app.model.EnterpriseInformationModel;
import com.app.model.PersonalInformationModel;
import com.app.model.UserModel;
import com.app.service.CustomerService;
import com.app.service.SubsidyApplicationDetailsService;
import com.app.service.UserService;
import com.app.util.Result;
import com.app.util.TokenUtils;
import com.app.vo.CUserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


@RestController
@RequestMapping("/subsidyApplicationDetails")
public class SubsidyApplicationDetailsController {
    @Autowired
    private SubsidyApplicationDetailsService subsidyApplicationDetailsService;
    @Autowired
    private UserService userService;

    /**
     * 小程序————新增一条申请记录
     * @param subsidyApplicationId subsidyApplication的Id 新增的时候传Id，充填的时候传0
     * @param subsidyApplicationDetailId subsidyApplicationDetailId的Id  新增的时候为0  重新提交的时候为对应的Id
     * @param request
     * @return
     */
    @PostMapping("/addAndUpdate")
    public Result addAndUpdate(HttpServletRequest request,
                               @RequestParam String personalInformationModel,
                               @RequestParam String enterpriseInformationModel,
                                           Integer subsidyApplicationDetailId,
                                           Integer subsidyApplicationId)  {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String mobile = TokenUtils.verify(token);
        UserModel user = userService.getUserByMobile(mobile);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        PersonalInformationModel personalInformationModel1 = JSON.parseObject(personalInformationModel, new TypeReference<PersonalInformationModel>() {});
        EnterpriseInformationModel enterpriseInformationModel1 = JSON.parseObject(enterpriseInformationModel, new TypeReference<EnterpriseInformationModel>() {});
        return subsidyApplicationDetailsService.add(user.getId(),enterpriseInformationModel1,personalInformationModel1,subsidyApplicationId,subsidyApplicationDetailId);
    }


    /**
     * 小程序————根据申请表Id查询申请详情表 审核状态进度
     */
    @GetMapping("/getSAStatusBySAId")
    public Result getSAStatusBySAId(Integer subsidyApplicationId) {
        return subsidyApplicationDetailsService.getSAStatusBySAId(subsidyApplicationId);
    }

    /**
     * 小程序————重新审核时根据Id查询
     * @param subsidyApplicationDetailId
     * @return
     */
    @GetMapping("/getDetailById")
    public Result getDetailById(Integer subsidyApplicationDetailId) {
        return subsidyApplicationDetailsService.getDetailById(subsidyApplicationDetailId);
    }



}
